local Town = GV:addState('Town')
function Town:enterState()
	print("entering town state")

	--build_town()
	print(table.tostring(meta_tiles))


	function init_game_town()

		data.player.x = (grid_size-4)*tile_size
		data.player.y = (grid_size/2)*tile_size
		data.player.image = packie_img
		data.player.startX = data.player.x
		data.player.startY = data.player.y
		stair_dh = maps.town.features.stair_dh
		stair_dh.x = data.player.startX
		stair_dh.y = data.player.startY
		stair_dh.image = env_sheet_quads[1]


	end


	if maps.town.l_times < 1 then
		init_game_town()
		place_npcs(maps.town.npcs)
		itemize_npcs(maps.town.npcs)

		data.player.local_map = maps.town
		local_map = data.player.local_map



	else

		data.player.local_map = maps.town
		local_map = data.player.local_map
		--print(table.tostring(town_layout))

	end



	--print("maps.town = ", table.tostring(maps.town.items))


	--print(table.tostring(cur_map))


	function love.draw()
		draw_town()
		draw_npcs(maps.town)
		draw_items()


		draw_buy()
		draw_drop()
		draw_equip_hand()

		draw_narrative(meta_tiles)

		draw_player()
		draw_hud()






	end

	local frame_count = 0

	function love.update(dt)

		global_updates(dt)

		--get_threads()
		local fps = love.timer.getFPS()
		local timer = fps * 3

		frame_count = frame_count + 1
		if frame_count == timer then
			clear_console()
			npc_tidy(maps.town.npcs)
			p_tidy()
			frame_count = 0
		end


	end

	function love.keypressed(key, unicode)

		--print(table.tostring(cur_map))
		if maps.town.l_times < 1 then
			basic_controls(key,unicode, town_layout, "")
		else
			basic_controls(key,unicode, town_layout, "")
		end

		if data.player.buy_mode == true
			and data.player.text_input == false then

			data.player.buy_mode = false
			for i = 1, #data.player.local_map.npcs do
				data.player.local_map.npcs[i].shopping = false

			end


		end

		if key == "b" then
			if data.player.buy_mode == false then
				for i = 1, #maps.town.npcs do
					if npc_check_fov(data.player.x,data.player.y, maps.town.npcs[i], npc_fov) then
						maps.town.npcs[i].shopping = true
						data.player.merchant_id = maps.town.npcs[i].shop_id
						print(maps.town.npcs[i].price_mod)
						npc_show_inv(maps.town.npcs[i])


					end

				end
			else
				data.player.buy_mode = false
				for i = 1, #maps.town.npcs do
					if maps.town.npcs[i].shopping == true then
						maps.town.npcs[i].shopping = false
					end
				end
			end

		end

		if key == "x" then

			for i = 1, #maps.town.npcs do
				if npc_check_fov(data.player.x,data.player.y,
					maps.town.npcs[i], npc_fov) then

					maps.town.npcs[i].shopping = true
					data.player.merchant_id = maps.town.npcs[i].shop_id
					if data.player.right_hand[1] ~= nil then
						sell_items(data.player.right_hand[1], data.player.price_mod, "right")
					elseif data.player.left_hand[1] ~= nil then
						sell_items(data.player.left_hand[1], data.player.price_mod, "left")
					end

				end

			end


			if maps.town.features.stair_dh.x == data.player.x
				and maps.town.features.stair_dh.y == data.player.y then
				if data.player.harvest_level == 0 then
					data.player.harvest_level = 1
					give_item(Hatchet, data.player)
					give_item(Pistol, data.player)
					give_item(Grenade, data.player)

				end
				data.player.hup = true
				data.player.current_harvest = 1--data.player.current_harvest + 1
				print("exiting town harvest : ", data.player.current_harvest)
				gv:gotoState('Harvest')
			end



		end

		if key == "q" then
			for i = 1, #maps.town.npcs do

				if npc_check_fov(data.player.x,data.player.y,
					maps.town.npcs[i], npc_fov) then

					if maps.town.npcs[i].q_given == false then
						npc_veggie_quest(maps.town.npcs[i])
					else
						p_quest_turn_in(maps.town.npcs[i])
					end

				end


			end


		end
	end



	function draw_town()


		for i = 1, #maps.town.tiles do
			draw_in_fov(maps.town.tiles[i])
		end

		love.graphics.drawq(env_sheet_img, stair_dh.image,
				stair_dh.x,stair_dh.y,0,2.0,2.0)




		--render_view(data.player, maps.town)

	end





end

function Town:exitState()
	print("exiting town state")

end
